package com.bcbim.api_model.file.controller;

import com.bcbim.api_model.component.controller.ComponentController;
import com.bcbim.authentication_interface.application.bo.ApplicationBO;
import com.bcbim.authentication_interface.application.service.ApplicationService;
import com.bcbim.authentication_interface.util.ApplicationAuthUtil;
import com.bcbim.common.response.annotation.Translate;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.sys.ParamException;
import com.bcbim.common.util.GuidUtil;
import com.bcbim.common.util.ParamCheck;
import com.bcbim.model_server_interface.file.dao.FileDAO;
import com.bcbim.model_server_interface.file.service.FileService;
import com.bcbim.model_server_interface.file.vo.FileInfoVO;
import com.bcbim.model_server_interface.file.vo.FileModelInfoVO;
import com.bcbim.model_server_interface.floor.service.FloorService;
import com.bcbim.model_server_interface.floor.vo.FloorVO;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: 模型文件接口
 * @Author: ye
 * @Date: 2022/3/24
 * @Version: V1.0
 **/
@RestController
@RequestMapping("/file")
public class FileController {

    @Value("${application.upload-path}")
    private String FILE_PATH;

    @Reference(protocol = "dubbo", group = "file", version = "1.0.0")
    private FileService fileServiceImpl;
    @Reference(protocol = "dubbo", group = "floor", version = "1.0.0")
    private FloorService floorServiceImpl;
    @Reference(protocol = "dubbo", group = "application", version = "1.0.0")
    private ApplicationService applicationServiceImpl;

    /**
     * @Description: 解析上传文件
     * @Param: 
     * @Return: 
     * @Author: ye
     * @Date: 2022/4/11
     */
    @Translate
    @PostMapping("uploadFile")
    public ReturnMsg saveFile(MultipartFile file) throws IOException {
        if (file == null) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        ApplicationBO application = ApplicationAuthUtil.getApplication();
        if (application.getResidualFlow() < (file.getSize() / 1048576)) {
            return ReturnMsg.consFail("剩余流量不足");
        }

        String id = GuidUtil.get();
        long timeMillis = System.currentTimeMillis();
        String path = FILE_PATH + "modelFile\\" + id;
        String filePath = path + "\\" + timeMillis + ".rvt";
        File dictionary = new File(path);
        File destFile = new File(filePath);
        dictionary.mkdirs();
        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            return ReturnMsg.consFail("上传失败");
        }
        String filename = file.getOriginalFilename();
        if (filename.length() > 200) {
            filename = filename.substring(0, 199);
        }

        String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1, filePath.lastIndexOf("."));
        File revitFile = new File(filePath);
        FileDAO fileDAO = new FileDAO();
        fileDAO.setId(GuidUtil.get());
        fileDAO.setFileName(filename);
        fileDAO.setSize(String.valueOf(revitFile.length()));
        fileDAO.setPath("\\resources\\modelFile\\" + id + "\\model\\" + fileName);
        fileDAO.setFileStatusType("1");
        fileDAO.setDel(Constant.NOT_DEL);
        fileDAO.setCreateTime(LocalDateTime.now());
        fileDAO.setApplicationId(ApplicationAuthUtil.getApplication().getId());
        fileServiceImpl.insertFile(fileDAO);

        fileServiceImpl.parsingFile(filePath, fileName, fileDAO, ApplicationAuthUtil.getApplication().getAppKey(), file.getSize() / 1048576);

        FileInfoVO info = new FileInfoVO(fileDAO);
        return ReturnMsg.consSuccess(info);
    }

    /**
     * @Description: 根据文件id查询文件信息
     * @Param: fileId
     * @Return: ReturnMsg
     * @Author: ye
     * @Date: 2022/4/14
     */
    @PostMapping("getFileById")
    public ReturnMsg getFileById(String fileId) {
        try {
            ParamCheck.idCheck(fileId);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        FileModelInfoVO info = fileServiceImpl.getFileModelById(fileId);
        return ReturnMsg.consSuccess(info);
    }

    /**
     * @Description: 查询楼层
     * @Param: fileId
     * @Return: ReturnMsg
     * @Author: ye
     * @Date: 2022/4/15
     */
    @PostMapping("listFloor")
    public ReturnMsg listFloor(String fileId) {
        try {
            ParamCheck.idCheck(fileId);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }

        List<FloorVO> list = floorServiceImpl.listFloor(fileId);
        return ReturnMsg.consSuccess(list);
    }

    /**
     * @Description: 查询文件信息
     * @Param:
     * @Return:
     * @Author: ye
     * @Date: 2022/5/6
     */
    @Translate
    @PostMapping("getFileInfo")
    public ReturnMsg getFileInfo(String[] fileIds) {
        if (ComponentController.checkArrId(fileIds)) return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);

        List<FileInfoVO> list = fileServiceImpl.getFileInfo(fileIds);
        return ReturnMsg.consSuccess(list);
    }
}
